﻿Public Class TypologieNivXssCatg

#Region "Declaration"
    Dim db As New Connexion
    Public indexPublic As Integer = 0
    Public Table1 As String
    Public Table2 As String
    Public databaseName As String

    Dim TextBoxNiv As String
    Dim TextBoxCodVar As String
    Dim TextBoxCodSSClass As String
    Dim TextBoxCodClass As String
    Dim TextBoxCodSSys As String
    Dim TextBoxCodSys As String

    Dim pas As String
    Dim Niveau As Int16
    Public OrdreInsertion As String
    Dim contextMenu1 As New ContextMenu
    Dim menuItem1 As New MenuItem("&Avant")
    Dim menuItem2 As New MenuItem("&Aprés")
    Dim menuItem3 As New MenuItem("&Annuler")
    Dim point1 As New System.Drawing.Point

    Public WithEvents WB_NAV As New WebBrowser
    Dim Entrer As String
    Dim LibelleNiv As String()
    Dim CodeNiv As String()
    Dim NomNiv As String()
    Dim IdNiv As String()
    Dim Nomtable As String()
    Dim NbrNiv As Integer = 0
#End Region
    Private Sub TypologieNivXssCatg_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        RadioButtonAp.Enabled = False
        RadioButtonAv.Enabled = False
        RadioButtonFin.Enabled = False
        Me.TextBoxDecoupageHab.Enabled = False
        Me.ButtonAjoutHab.Enabled = False


        db.myReq = "SELECT COUNT(syscolumns.name) AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= '" & Table2 & "'"
        db.Connecter(databaseName)
        While db.rdr.Read()
            NbrNiv = db.rdr(0) - 6
        End While
        db.Disconnecter()
        MsgBox("Intial" & NbrNiv + 6)
        LibelleNiv = New String(NbrNiv - 1) {}
        CodeNiv = New String(NbrNiv - 1) {}
        NomNiv = New String(NbrNiv - 1) {}
        IdNiv = New String(NbrNiv - 1) {}
        Nomtable = New String(NbrNiv + 6) {}
        Dim iter As Integer = 1
        While iter < NbrNiv
            db.myReq = "SELECT [Libelle_Niveau] FROM  [dbo].[Niveau] Where [Table_Niveau]='" & Table2 & "' and [Niv_Niveau]='" & iter & "'"
            db.Connecter(databaseName)
            While db.rdr.Read()
                LibelleNiv(iter - 1) = db.rdr(0)
                MsgBox(LibelleNiv(iter - 1))
            End While
            db.Disconnecter()
            iter = iter + 1
        End While

        db.myReq = "SELECT syscolumns.name AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= '" & Table2 & "'"
        db.Connecter(databaseName)
        iter = 0
        While db.rdr.Read()
            Nomtable(iter) = db.rdr(0)
            iter = iter + 1
        End While
        db.Disconnecter()

        Niveau = 1
        pas = "010"
        Entrer = ""
        DataGridViewTypeHab.MultiSelect = False
        DataGridViewTypeHab.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridViewTypeHab.ReadOnly = True

        miseAjour()

        'CreateColumn()

    End Sub

    Private Sub CreateColumn()
        DataGridViewTypeHab.Columns.Clear()
        DataGridViewTypeHab.ColumnCount = 7

        ' Initialize the button column.

        Dim IDX As New DataGridViewTextBoxColumn
        With IDX
            .HeaderText = "IDX"
            .Name = "IDX"
            .Visible = True
        End With
        DataGridViewTypeHab.Columns.Insert(0, IDX)

        Dim IDCatg As New DataGridViewTextBoxColumn
        With IDCatg
            .HeaderText = "IDCatg"
            .Name = "IDCatg"
            .Visible = True
        End With
        DataGridViewTypeHab.Columns.Insert(1, IDCatg)

        Dim IDNiv As New DataGridViewTextBoxColumn
        With IDNiv
            .HeaderText = "IDNiv"
            .Name = "IDNiv"
            .Visible = True
        End With
        DataGridViewTypeHab.Columns.Insert(2, IDNiv)

        Dim CodeX As New DataGridViewTextBoxColumn
        With CodeX
            .HeaderText = "Code"
            .Name = "Code"
            .Visible = True
        End With
        DataGridViewTypeHab.Columns.Insert(3, CodeX)

        'Dim iter As Integer = 1
        'While iter < NbrNiv
        '    Dim TextBoxColumn As New DataGridViewTextBoxColumn
        '    With TextBoxColumn
        '        .HeaderText = "CodeX" & iter
        '        .Name = "CodeX" & iter
        '        .Visible = True
        '    End With
        '    DataGridViewTypeHab.Columns.Insert(iter + 3, TextBoxColumn)
        '    iter = iter + 1
        'End While

        Dim NivX As New DataGridViewTextBoxColumn
        With NivX
            .HeaderText = "NivX"
            .Name = "NivX"
            .Visible = True
        End With
        DataGridViewTypeHab.Columns.Insert(NbrNiv + 4, NivX)

        Dim NomX As New DataGridViewTextBoxColumn
        With NomX
            .HeaderText = "Nom"
            .Name = "Nom"
            .Visible = True
        End With
        DataGridViewTypeHab.Columns.Insert(NbrNiv + 5, NomX)

        Dim ExpX As New DataGridViewTextBoxColumn
        With ExpX
            .HeaderText = "ExpX"
            .Name = "ExpX"
            .Visible = True
        End With
        DataGridViewTypeHab.Columns.Insert(NbrNiv + 6, ExpX)

        'Dim buttonColumn As New DataGridViewButtonColumn

        'With buttonColumn
        '    .HeaderText = "Details"
        '    .Name = "Details"
        '    .Text = "View Details"
        '    .UseColumnTextForButtonValue = True
        'End With
        'DataGridViewTypeHab.Columns.Insert(NbrNiv + 7, buttonColumn)

    End Sub



    Private Sub initialisation()

        Me.TextBoxDecoupageHab.Text = "Découpage de la typologie d'habitat"
        LabelNiv.Text = LibelleNiv(0)
        Niveau = 1
        Entrer = ""

        Dim i As Integer = 0
        While i < NbrNiv
            CodeNiv(i) = ""
            IdNiv(i) = ""
            i = i + 1
        End While

        Me.TextBoxCodeHab.Text = ""
        Me.TextBoxNomHab.Text = ""
        Me.TextBoxDefHab.Text = ""

    End Sub


    Private Sub miseAjour()
        Entrer = ""
        Dim requeteX As String
        Dim niv As Integer = 1


        requeteX = "Select * from  [dbo].[" & Table2 & "] WHERE " & Nomtable(3 + NbrNiv) & " = '" & Niveau & "' "

        While niv < Niveau
            requeteX = requeteX & "and " & Nomtable(2 + niv) & " = '" & CodeNiv(niv - 1) & "' "
            niv = niv + 1
        End While

        ' MsgBox(requeteX)

        If Niveau = 1 Then
            Me.ButtonBack.Enabled = False
            Me.ButtonNext.Enabled = True
        ElseIf Niveau = NbrNiv Then
            Me.ButtonBack.Enabled = True
            Me.ButtonNext.Enabled = False
        Else
            Me.ButtonBack.Enabled = True
            Me.ButtonNext.Enabled = True
        End If

        db.myReq = requeteX
        db.Connecter(databaseName)
        DataGridViewTypeHab.Rows.Clear()
        While db.rdr.Read()
            Dim iterCode As Integer = 0
            Dim result As String = ""

            'result = db.rdr(0) & "," & db.rdr(1) & "," & db.rdr(2) & "," & db.rdr(3)

            'While iterCode < NbrNiv
            '    result = result & "," & db.rdr(4 + iterCode)
            '    iterCode = iterCode + 1
            'End While
            'result = result & "," & db.rdr(NbrNiv + 4) & "," & db.rdr(NbrNiv + 5) & "," & db.rdr(NbrNiv + 6)

            DataGridViewTypeHab.Rows.Add(db.rdr(0), db.rdr(2), db.rdr(NbrNiv + 4), db.rdr(NbrNiv + 5), db.rdr(NbrNiv + 6))

        End While
        db.Disconnecter()

        If DataGridViewTypeHab.RowCount - 1 = 0 Or Niveau = NbrNiv Then
            Me.TextBoxCodeHab.Text = ""
            Me.TextBoxNomHab.Text = ""
            Me.TextBoxDefHab.Text = ""
            Me.ButtonAjoutHab.Enabled = False
            Me.RadioButtonAp.Enabled = False
            Me.RadioButtonAv.Enabled = False
            Me.RadioButtonFin.Enabled = False
            Me.ButtonNext.Enabled = False
        Else
            Me.TextBoxCodeHab.Text = ""
            Me.TextBoxNomHab.Text = ""
            Me.TextBoxDefHab.Text = ""
            'DataGridViewTypeHab.Rows(0).Selected = True
            'Me.TextBoxCodeHab.Text = DataGridViewTypeHab.SelectedCells(8).Value
            'Me.TextBoxNomHab.Text = DataGridViewTypeHab.SelectedCells(9).Value
            'Me.TextBoxDefHab.Text = DataGridViewTypeHab.SelectedCells(10).Value
            'Me.TextBoxID.Text = DataGridViewTypeHab.SelectedCells(0).Value
            Me.ButtonAjoutHab.Enabled = True
            Me.ButtonNext.Enabled = True
        End If

        If Not DataGridViewTypeHab.RowCount - 1 = 0 Then
            Me.ButtonAjoutHab.Enabled = True
        End If


    End Sub

    Private Sub DataGridViewTypeHab_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewTypeHab.CurrentCellChanged
        If Entrer = "debut" Then
            selection()
        End If
    End Sub
    Private Sub DataGridViewTypeHab_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridViewTypeHab.MouseClick
        If e.Button = MouseButtons.Left Then
            Entrer = "debut"
            selection()
        End If
    End Sub


    Private Sub selection()
        Dim niv As Integer = 0
        Me.TextBoxDecoupageHab.Text = ""
        If Not DataGridViewTypeHab.SelectedCells(2).Value.ToString = "" Then

            NomNiv(Niveau - 1) = DataGridViewTypeHab.SelectedCells(2).Value

            While niv < NbrNiv
                Me.TextBoxDecoupageHab.Text = Me.TextBoxDecoupageHab.Text & NomNiv(niv)
                niv = niv + 1
            End While

            Me.TextBoxCodeHab.Text = DataGridViewTypeHab.SelectedCells(1).Value
            Me.TextBoxNomHab.Text = DataGridViewTypeHab.SelectedCells(2).Value
            Me.TextBoxDefHab.Text = DataGridViewTypeHab.SelectedCells(3).Value
            Me.oidValue.Text = DataGridViewTypeHab.SelectedCells(4).Value
            IdNiv(Niveau - 1) = DataGridViewTypeHab.SelectedCells(0).RowIndex

        End If
    End Sub

    Private Sub ButtonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNext.Click
        Entrer = ""

        Niveau = Niveau + 1
        'Me.LabelNiv.Text = LibelleNiv(Niveau)

        db.myReq = "Select * from  [dbo].[" & Table2 & "] WHERE " & Nomtable(0) & " = '" & DataGridViewTypeHab.SelectedCells(0).Value & "'"
        db.Connecter(databaseName)
        While db.rdr.Read
            CodeNiv(Niveau - 2) = db.rdr(1 + Niveau)
            MsgBox(CodeNiv(Niveau - 2))
        End While
        Me.RadioButtonAp.Checked = False
        Me.RadioButtonAv.Checked = False
        Me.RadioButtonFin.Checked = False

        miseAjour()
    End Sub

    Private Sub ButtonBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBack.Click
        Dim niv As Integer = 0
        Me.TextBoxDecoupageHab.Text = ""
        Entrer = ""
        Niveau = Niveau - 1
        Me.LabelNiv.Text = LibelleNiv(Niveau - 1)
        While niv < Niveau
            Me.TextBoxDecoupageHab.Text = Me.TextBoxDecoupageHab.Text & NomNiv(niv)
            niv = niv + 1
        End While
        miseAjour()
        DataGridViewTypeHab.Rows(IdNiv(Niveau - 1)).Selected = True
    End Sub

    Private Sub ButtonAjoutHab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAjoutHab.Click

        Me.RadioButtonAp.Enabled = True
        Me.RadioButtonAv.Enabled = True
        Me.RadioButtonFin.Enabled = True

        Me.TextBoxCodeHab.Text = ""
        Me.TextBoxNomHab.Text = ""
        Me.TextBoxDefHab.Text = ""

    End Sub


    Private Sub ButtonEnregistrer_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click

        Dim Str As String
        Dim var1, var2, var3, index, MaxcodeR As Int16
        Dim Code, code2, CodeGlobal, requete As String
        CodeGlobal = ""
        Code = 0
        code2 = ""
        MaxcodeR = 0



        'Récupérer le code du niveau et indexe de la ligne sélectionnée
        If Me.DataGridViewTypeHab.RowCount - 1 > 0 Then
            db.myReq = "Select " & Nomtable(2 + Niveau) & " from  [dbo].[" & Table2 & "] WHERE " & Nomtable(0) & " = '" & Me.DataGridViewTypeHab.SelectedCells(0).Value & "'"
            db.Connecter(databaseName)
            While db.rdr.Read
                Code = db.rdr(0)
            End While
            db.Disconnecter()
            index = DataGridViewTypeHab.SelectedCells(0).RowIndex
        End If


        'Insérer après la ligne sélectionnée
        If RadioButtonAp.Checked Then
            If (DataGridViewTypeHab.RowCount - 2) = index Then
                var1 = Code
                var2 = pas
                var3 = (var1 + var2)
            Else
                db.myReq = "Select " & Nomtable(2 + Niveau) & " from  [dbo].[" & Table2 & "] WHERE " & Nomtable(0) & " = '" & DataGridViewTypeHab.Rows(index + 1).Cells(0).Value & "'"
                db.Connecter(databaseName)
                While db.rdr.Read
                    code2 = db.rdr(0)
                End While
                db.Disconnecter()

                var1 = Code
                var2 = code2
                var3 = (var1 + var2) / 2
            End If
        End If

        'Insérer avant la ligne sélectionnée
        If RadioButtonAv.Checked Then
            If index = 0 Then
                code2 = 0
            Else
                db.myReq = "Select " & Nomtable(2 + Niveau) & " from  [dbo].[" & Table2 & "] WHERE " & Nomtable(0) & " = '" & DataGridViewTypeHab.Rows(index - 1).Cells(0).Value & "'"
                MsgBox(db.myReq)
                db.Connecter(databaseName)
                While db.rdr.Read
                    code2 = db.rdr(0)
                End While
                db.Disconnecter()
            End If
            var1 = Code
            var2 = code2
            var3 = (var1 + var2) / 2
        End If

        'Insérer à la fin
        If RadioButtonFin.Checked Then
            Dim CodeAct As Integer
            db.myReq = "Select MAX(" & Nomtable(2 + Niveau) & " ) from  [dbo].[" & Table2 & "]"
            MsgBox(db.myReq)
            db.Connecter(databaseName)
            If Not IsDBNull(db.rdr) Then
                CodeAct = db.rdr(0)
                var3 = pas + CodeAct
            Else
                var3 = pas
            End If
            db.Disconnecter()
        End If

        'Le cas de la première insertion
        If Not Me.ButtonAjoutHab.Enabled Then
            var3 = pas
        End If

        If var3 < 1 Or var3 > 990 Then
            majDB()
        End If

        'Convertir la valeur du code (Système /SS Sys/Classe / SS classe / Variante) en String sous la forme xxx
        var3 = Int(var3)
        If var3.ToString.Length = 2 Then
            Str = "0" & var3.ToString
        ElseIf var3.ToString.Length = 1 Then
            Str = "00" & var3.ToString
        Else
            Str = var3.ToString
        End If

        Dim iterCode As Integer = 0
        'Inserer les valeurs des codes
        CodeNiv(Niveau - 1) = Str
        iterCode = Niveau
        While iterCode < NbrNiv
            CodeNiv(iterCode) = "000"
            iterCode = iterCode + 1
        End While


        Dim result As String
        iterCode = 0
        result = Nomtable(1) & "," & Nomtable(2)

        While iterCode < NbrNiv
            CodeGlobal = CodeGlobal & CodeNiv(iterCode)
            result = result & "," & Nomtable(3 + iterCode)
            iterCode = iterCode + 1
        End While
        result = result & "," & Nomtable(NbrNiv + 3) & "," & Nomtable(NbrNiv + 4) & "," & Nomtable(NbrNiv + 5) & "," & Nomtable(NbrNiv + 6)

        requete = " INSERT INTO  [dbo].[" & Table2 & "](" & result & ") VALUES('1' ,'" & Me.TextBoxCodeHab.Text & "'"

        iterCode = 0
        While iterCode < NbrNiv
            requete = requete & " ,'" & CodeNiv(iterCode) & "'"
            iterCode = iterCode + 1
        End While
        'requete = requete & ",'" & Niveau & " ','" & Me.TextBoxNomHab.Text & "','','" & Me.TextBoxDefHab.Text & "')"
        requete = requete & ",'" & Niveau & "','" & Me.TextBoxNomHab.Text & "','" & Me.TextBoxDefHab.Text & "','" & Me.oidValue.Text & "')"
        'Ajouter l'enregistrement dans la BD
        ' db.myReq = " INSERT INTO  [ "  & databaseName & "].[dbo].[" & Table2 & "](" & result & ") VALUES('" & ComboBoxIdTypol.Text & "', '' ,'" & CodeGlobal & "','" & valeur & "','" & Niveau & "','" & Me.TextBoxNomHab.Text & "','','" & Me.TextBoxDefHab.Text & "')"
        db.myReq = requete
        MsgBox(requete)
        db.Connecter(databaseName)
        db.Disconnecter()
        miseAjour()

    End Sub

    Private Sub majDB()
        Throw New NotImplementedException
    End Sub

    ''' <summary>
    ''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''' </summary>
    ''' <remarks></remarks>
    ''' 
    Private _workspace As Object
    Private _layerName As String
    Private geoForm As GeoLocation = Nothing
    Property layerName() As String
        Get
            Return _layerName
        End Get
        Set(ByVal value As String)
            _layerName = value
        End Set
    End Property

    Property workspace() As Object
        Get
            Return _workspace
        End Get
        Set(ByVal value As Object)
            _workspace = value
        End Set
    End Property

    Private Sub btnGetGeo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetGeo.Click
        If geoForm IsNot Nothing AndAlso geoForm.Visible Then
            Return
        End If

        geoForm = New GeoLocation()
        geoForm.Show()
        geoForm.workspace = Me.workspace
        geoForm.oidParentText = oidValue

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            db.myReq = " DELETE FROM  [dbo].[" & Table2 & "]  WHERE " & Nomtable(0) & "= '" & Me.DataGridViewTypeHab.SelectedCells(0).Value & "'"
            db.Connecter(databaseName)
            db.Disconnecter()
            miseAjour()
            If DataGridViewTypeHab.RowCount - 1 = 0 Then
                Return
            End If
            Dim listOid As List(Of String) = New List(Of String)

            listOid.Add(DataGridViewTypeHab.SelectedCells(4).Value)

            _workspace.deleteNodes(_layerName, listOid)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ButtonModifier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonModifier.Click

    End Sub
End Class